function[]=simulregress(fce,a,b,n,r)
%SIMULREGRESS  simulation of the regression model
%
%  SIMULREGRESS starts a window with menu for simulation of the regression model
%
% (C) Jan Kolacek, Masaryk University (Czech Republic)

if nargin<1
    
if ~exist('w.mat','file')
    inic=[];save w.mat inic;
end
    
callbackStr=['set(gcf,''CloseRequestFcn'',''closereq'');',...
      'clear;load puvprom;',...
      'delete(gcf);'];

crStr= 'save puvprom;';
% unts = get(0,'Units');
% set(0,'Units','Normalized');
% scrsz = get(0,'ScreenSize');
% set(0,'Units',unts);
% 
simfig=figure( ...
	'Visible','on', ...
   'Name','SIMULATION OF REGRESSION MODEL', ...
   'CloseRequestFcn',callbackStr,...
   'Units','Normalized',...
   'Tag','kclos',...
   'CreateFcn',crStr,...
   'NumberTitle','off');

axes('Units','normalized', ...
     'Position',[0.08 0.08 0.62 0.67]);

top=.85;
fsz=0.31;  %FontSize

    prom=char(who('-file','puvprom.mat'));
    S=whos('-file','puvprom.mat');
    idtxt=zeros(length(S),1);
    iddbl=zeros(length(S),1);
    for i=1:length(S)
        idtxt(i)=strcmp(S(i).class,'char');
        iddbl(i)=strcmp(S(i).class,'double');
    end
    promtxt=prom(logical(idtxt),:);
   
    if size(promtxt,1)<1
        rozh='off';
        promtxt=char('[]');
    else
        rozh='on';
    end

uicontrol( ...
        'Style','text', ...
        'Units','normalized', ...
        'FontUnits','normalized',...
        'Position',[.08,top,.45,.1], ...
        'HorizontalAlignment','right',...
        'BackgroundColor',[0.8 0.8 0.8], ...
        'ForegroundColor',[0 0 0], ...
        'FontSize',fsz,...
        'String','Write the name of the regression function:');

    call=['G=get(gcf,''userdata'');m_def=get(G(1),''String'');',...
        'if ~isempty(m_def) set(G(6),''Enable'',''on'');else set(G(6),''Enable'',''off'');end;'];
    call1='GG=get(findobj(0,''Tag'',''defprom''),''userdata'');if ~isempty(GG) set(GG(3),''String'',m_def);end;';
    call2=['prom=get(GG(4),''String'');prom=pridej(prom,''m_def'');',...
        'set(GG(4),''String'',prom,''Enable'',''on'',''Value'',size(prom,1));'];

tHndl=uicontrol( ...
        'Style','edit', ...
        'Units','normalized', ...
        'FontUnits','normalized',...
        'Position',[.55,top+.05,.15,.05], ...
        'String','', ...
        'HorizontalAlignment','left',...
        'Callback',[call,call1,call2]);
%        'CreateFcn','m_def=[];',...

uicontrol( ...
        'Style','text', ...
        'Units','normalized', ...
        'FontUnits','normalized',...
        'Position',[.08,top-.08,.45,.1], ...
        'HorizontalAlignment','right',...
        'BackgroundColor',[0.8 0.8 0.8], ...
        'ForegroundColor',[0 0 0], ...
        'FontSize',fsz,...
        'String','or load the name from a variable:');
    
    call1='G=get(gcf,''userdata'');ind=get(G(2),''Value'');prom=get(G(2),''String'');';
    call2='m_def=eval(prom(ind,:));set(G(1),''String'',m_def);';
    call3='if ~isempty(m_def) set(G(6),''Enable'',''on'');else set(G(6),''Enable'',''off'');end;';
    call4=['GG=get(findobj(0,''Tag'',''defprom''),''userdata'');if ~isempty(GG)',... 
        'set(GG(3),''String'',m_def);',...
        'set(GG(4),''String'',prom,''Value'',ind,''Enable'',''on'');end;'];

    rHndl=uicontrol( ...
        'Style','popup', ...
        'Enable',rozh,...
        'Units','normalized', ...
        'FontUnits','normalized',...
        'Position',[.6,top-.02,.1,.04], ...
        'String',promtxt, ...
        'FontSize',2*fsz+.1,...
        'Value',1,...
        'Callback',[call1,call2,call3,call4]);
%        'CreateFcn',['m_def=',prom(1,:),';'],...

uicontrol( ...
        'Style','text', ...
        'Units','normalized', ...
        'FontUnits','normalized',...
        'Position',[.75,top,.25,.1], ...
        'HorizontalAlignment','left',...
        'BackgroundColor',[0.8 0.8 0.8], ...
        'ForegroundColor',[0 0 0], ...
        'FontSize',fsz,...
        'String','Set the interval [a,b]');

uicontrol( ...
        'Style','text', ...
        'Units','normalized', ...
        'FontUnits','normalized',...
        'Position',[.77,top-.08,.05,.1], ...
        'HorizontalAlignment','left',...
        'BackgroundColor',[0.8 0.8 0.8], ...
        'ForegroundColor',[0 0 0], ...
        'FontSize',fsz,...
        'String','a:');
    
call=['G=get(gcf,''userdata'');a=str2num(get(G(3),''String''));',...
        'G(end-3)=a;axis(G(end-3:end));set(gcf,''userdata'',G);putsigma;'];

aHndl=uicontrol( ...
        'Style','edit', ...
        'Units','normalized', ...
        'FontUnits','normalized',...
        'Position',[.8,top-.03,.04,.05], ...
        'String','0', ...
        'HorizontalAlignment','left',...
        'FontSize',2*fsz,...
        'CreateFcn','a=0;',...
        'Callback',call);

uicontrol( ...
        'Style','text', ...
        'Units','normalized', ...
        'FontUnits','normalized',...
        'Position',[.87,top-.08,.05,.1], ...
        'HorizontalAlignment','left',...
        'BackgroundColor',[0.8 0.8 0.8], ...
        'ForegroundColor',[0 0 0], ...
        'FontSize',fsz,...
        'String','b:');
    
call=['G=get(gcf,''userdata'');b=str2num(get(G(4),''String''));',...
        'G(end-2)=b;axis(G(end-3:end));set(gcf,''userdata'',G);putsigma;'];
bHndl=uicontrol( ...
        'Style','edit', ...
        'Units','normalized', ...
        'FontUnits','normalized',...
        'Position',[.9,top-.03,.04,.05], ...
        'String','1', ...
        'HorizontalAlignment','left',...
        'FontSize',2*fsz,...
        'CreateFcn','b=1;',...
        'Callback',call);

uicontrol( ...
        'Style','text', ...
        'Units','normalized', ...
        'FontUnits','normalized',...
        'Position',[.75,top-.19,.2,.1], ...
        'HorizontalAlignment','left',...
        'BackgroundColor',[0.8 0.8 0.8], ...
        'ForegroundColor',[0 0 0], ...
        'FontSize',fsz,...
        'String','Set number of design points:');

call='G=get(gcf,''userdata'');n=str2num(get(G(9),''String''));';

nHndl=uicontrol( ...
        'Style','edit', ...
        'Units','normalized', ...
        'FontUnits','normalized',...
        'Position',[.9,top-.18,.04,.05], ...
        'String','100', ...
        'HorizontalAlignment','left',...
        'FontSize',2*fsz,...
        'CreateFcn','n=100;',...
        'Callback',call);

    cellstr={'Set the variance'};
uicontrol( ...
        'Style','text', ...
        'Units','normalized', ...
        'FontUnits','normalized',...
        'Position',[.75,top-.3,.2,.05], ...
        'HorizontalAlignment','left',...
        'BackgroundColor',[0.8 0.8 0.8], ...
        'ForegroundColor',[0 0 0], ...
        'FontSize',2*fsz,...
        'String',cellstr);

cstr(1)={' \sigma ^2:'};
text(1.27,top-.19,cstr,'FontUnits','Normalized',...
    'Fontsize',.05,'HorizontalAlignment','center','Tag','sigma');

call='G=get(gcf,''userdata'');s=str2num(get(G(5),''String''));';
        
vrcHndl=uicontrol( ...
        'Style','edit', ...
        'Units','normalized', ...
        'FontUnits','normalized',...
        'Position',[.9,top-.36,.04,.05], ...
        'String','0.1', ...
        'HorizontalAlignment','left',...
        'FontSize',2*fsz,...
        'CreateFcn','s=0.1;',...
        'Callback',call);

%The SIMULATE button
call=['delete(gca);axes(''Units'',''normalized'',''Position'',[0.08 0.08 0.62 0.67]);',...
    '[x_def,y_def]=rozhaz(m_def,a,b,n,s,gcf);G=get(gcf,''userdata'');',...
    'set(G(7),''enable'',''on'');save puvprom.mat x_def y_def m_def s -append;'];
call1=['GG=get(findobj(0,''Tag'',''defprom''),''userdata'');if ~isempty(GG)',... 
    'prom=get(GG(1),''String'');prom=pridej(prom,''x_def'');',...
    'set(GG(1),''String'',prom,''Enable'',''on'',''Value'',1);',...   
    'prom=get(GG(2),''String'');prom=pridej(prom,''y_def'');',...
    'set(GG(2),''String'',prom,''Enable'',''on'',''Value'',1);',...
    'set(GG(5),''Enable'',''on'');end;'];   

simulHndl=uicontrol( ...
        'Style','push', ...
        'Enable','off',...
        'Units','normalized', ...
        'FontUnits','normalized',...
        'Position',[0.8,top-.5,.1,0.1], ...
        'String','Simulate', ...
        'FontSize',fsz,...
        'Callback',[call,call1]);

% The SAVE text     
uicontrol( ...
	     'Style','text', ...
        'Units','normalized', ...
        'FontUnits','normalized',...
        'HorizontalAlignment','left',...
        'Position',[.8,.26,.2,.05], ...
        'BackgroundColor',[0.8 0.8 0.8], ...
        'FontSize',2*fsz,...
        'String','Save Data:');

% The VAR button
call=['checkLabels = {''Save design points to variable named:'','...
               '''Save measurements to variable named:'',',... 
               '''Save regression function to variable named:'',',... 
               '''Save variance of model to variable named:''};',...
'varNames = {''x_sim'',''y_sim'',''m'',''s2''};',... 
'items = {x_def,y_def,m_def,s};save ppne x_def y_def m_def s;clear x_def y_def m_def s;',...
'save ppp;save pp checkLabels varNames items;clear;load pp;delete pp.mat;',...
'[hdialog,ok_pressed]=export2wsdlg(checkLabels,varNames,items,''Save Data'');'...
'if ok_pressed G=get(gcf,''userdata'');set(G(8),''enable'',''on'');',...
'clear G hdialog ok_pressed checkLabels varNames items;varNames=who;',...
'save w varNames -append;save(''puvprom'',varNames{:},''-append'');',...
'else load ppne;end;load ppp;delete ppp.mat;delete ppne.mat;'];
    labelStr='var';
    varHndl=uicontrol( ...
        'Style','push', ...
        'Enable','off',...
        'Units','normalized', ...
        'FontUnits','normalized',...
        'Position',[0.8,.21,.05,0.05], ...
        'String',labelStr, ...
        'FontSize',2*fsz,...
        'Callback',call);

% The FILE button
    labelStr='file';
    call='load w; uisave(varNames,''data_sim'')';
    fileHndl=uicontrol( ...
        'Style','push', ...
        'Enable','off',...
        'Units','normalized', ...
        'FontUnits','normalized',...
        'Position',[0.85,.21,.05,0.05], ...
        'String',labelStr, ...
        'FontSize',2*fsz,...
        'Callback',call);

% The CLOSE button
    labelStr='Close';
 uicontrol( ...
        'Style','push', ...
        'Units','normalized', ...
        'FontUnits','normalized',...
        'Position',[0.84,.07,.1,0.1], ...
        'String',labelStr, ...
        'FontSize',fsz,...
        'Callback',callbackStr);
     
osy=[0 1 0 1];
hndlList=[tHndl,rHndl,aHndl,bHndl,vrcHndl,simulHndl,varHndl,fileHndl,nHndl,osy];
     set(gcf,'UserData',hndlList);    
     %set(C,'Position',[0 0.0347 1.0000 0.9190]);
     set(simfig,'Position',[0.1059 0.1655 0.7700 0.6898]);
     %set(C,'Position',[1 1 .97 .9].*scrsz+[.01 .012 0 0]);

else
    rozhaz(fce,a,b,n,r);
end